<template>
  <el-row :gutter="24" :style="{ paddingRight: collapseTree ? 'var(--mian-left-width)' : '0' }">
    <!-- 左侧部门树 -->
    <el-col :span="4" :xs="24">
      <ContentWrap class="h-[calc(100vh-var(--heard-height))]" :isTree="true" v-if="!collapseTree">
        <DeptTree @node-click="handleDeptNodeClick" />
      </ContentWrap>
    </el-col>
    <el-col :span="collapseTree ? 24 : 20" :xs="24">
      <ContentWrap ref="form">
        <el-form
          class="-mb-15px"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="设备名称" prop="name">
            <el-input
              v-model.tirm="queryParams.name"
              placeholder="请输入设备名称"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item label="mac" prop="mac">
            <el-input
              v-model.tirm="queryParams.mac"
              placeholder="请输入mac"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item label="设备类型" prop="type">
            <el-select
              v-model="queryParams.type"
              placeholder="请选择设备类型"
              clearable
              class="!w-240px"
            >
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_DEVICE_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="请选择状态"
              clearable
              class="!w-240px"
            >
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="sn" prop="sn">
            <el-input
              v-model.tirm="queryParams.sn"
              placeholder="请输入sn"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item label="imei" prop="imei">
            <el-input
              v-model.tirm="queryParams.imei"
              placeholder="请输入imei"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker
              v-model="queryParams.createTime"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item>
            <el-button @click="handleQuery">
              <Icon icon="ep:search" class="mr-5px" />
              搜索
            </el-button>
            <el-button @click="resetQuery">
              <Icon icon="ep:refresh" class="mr-5px" />
              重置
            </el-button>
            <!--            <el-button-->
            <!--              type="primary"-->
            <!--              plain-->
            <!--              @click="openForm('create')"-->
            <!--              v-hasPermi="['system:device:create']"-->
            <!--            >-->
            <!--              <Icon icon="ep:plus" class="mr-5px" />-->
            <!--              新增-->
            <!--            </el-button>-->
            <el-button
              type="success"
              plain
              @click="handleExport"
              :loading="exportLoading"
              v-hasPermi="['system:device:export']"
            >
              <Icon icon="ep:download" class="mr-5px" />
              导出
            </el-button>
          </el-form-item>
        </el-form>
      </ContentWrap>

      <!-- 列表 -->
      <ContentWrap :setPadding="false">
        <el-table v-loading="loading" :data="list" stripe :max-height="tableHeight">
          <el-table-column label="部门" prop="deptName" show-overflow-tooltip />
          <el-table-column label="设备名称" prop="name" show-overflow-tooltip />
          <el-table-column label="mac" prop="mac" show-overflow-tooltip />
          <el-table-column label="设备类型" prop="type">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.SYSTEM_DEVICE_TYPE" :value="scope.row.type" />
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="status">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="sn" prop="sn" show-overflow-tooltip />
          <el-table-column label="imei" prop="imei" show-overflow-tooltip />
          <el-table-column label="备注" prop="remark" show-overflow-tooltip />
          <el-table-column
            label="创建时间"
            prop="createTime"
            :formatter="dateFormatter"
            width="180"
            show-overflow-tooltip
          />
          <el-table-column label="操作" fixed="right" width="100">
            <template #default="scope">
              <el-button
                link
                type="primary"
                @click="openForm('update', scope.row.id)"
                v-hasPermi="['system:device:update']"
              >
                编辑
              </el-button>
              <el-button
                link
                type="danger"
                @click="handleDelete(scope.row.id)"
                v-hasPermi="['system:device:delete']"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </ContentWrap>
    </el-col>
  </el-row>

  <!-- 表单弹窗：添加/修改 -->
  <DeviceForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts" name="Device">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as DeviceApi from '@/api/system/device'
import DeviceForm from './DeviceForm.vue'
import { useAppStore } from '@/store/modules/app'
const appStore = useAppStore()
const collapseTree = computed(() => appStore.getCollapseTree)

const form = ref(null)
const { width, height } = useElementSize(form)
const tableHeight = useWindowSize(78 + 76, height)

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  createTime: [],
  sn: null,
  imei: null,
  name: null,
  type: null,
  status: null,
  mac: null,
  deptId: null
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DeviceApi.getDevicePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  queryParams.deptId = undefined
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DeviceApi.deleteDevice(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DeviceApi.exportDevice(queryParams)
    download.excel(data, '设备.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 处理部门被点击 */
const handleDeptNodeClick = async (row) => {
  queryParams.deptId = row.id
  await getList()
}

/** 初始化 **/
onMounted(() => {
  getList()
})

/* 切换页面展开当前页面树 */
onUnmounted(() => {
  appStore.setCollapseTree(false)
})
</script>
